home *** CD-ROM | disk | FTP | other *** search
Text File | 1986-12-31 | 15.7 KB | 659 lines | [TEXT/MPS ] |
- {Copyright © 1986 by Apple Computer, Inc. All Rights Reserved.}
-
- {UDemoText.inc1.p}
-
-
- CONST
-
- {Menu id's}
- mFont = 6;
- mStyle = 7;
-
-
- {Command numbers for font-size commands}
- cSizeChange = 1100;
- cSizeBase = 1100;
- cSizeMin = 1109;
- cSizeMax = 1124;
- {1101-1199 reserved for font sizes 1-99 pts.}
-
-
- {Command numbers for typestyle attributes}
- cStyleChange = 1200;
- cPlainText = 1201;
- cBold = 1202;
- cItalic = 1203;
- cUnderline = 1204;
- cOutline = 1205;
- cShadow = 1206;
- cCondense = 1207;
- cExtend = 1208;
-
- {Command numbers to cover other stylistic changes}
- cJustChange = 1300;
- cFontChange = 1301;
-
- {Constant for staggering windows}
- kStaggerAmount = 16;
-
- {Constants for the text specs resource}
- kTextSpecsRsrcType = 'SPEC';
- kTextSpecsRsrcID = 1;
-
- {Constants for the print info resource}
- kPrintInfoRsrcType = 'PRNT';
- kPrintInfoRsrcID = 1;
-
- {The 'File is too large' alert}
- kFileTooBig = 1000;
-
- VAR
- gFontNum: INTEGER; {Font number - default for new documents}
- gFontSize: INTEGER; {Font Size in points - default for new documents}
- gStaggerCount: INTEGER;
-
-
- PROCEDURE OutlineFontSizes(fontNum: INTEGER); FORWARD;
- {Makes the right Menu Manager calls so that the 'Font' menu has
- fontsizes representing 'natural fonts' shown in 'outline', and
- sizes which can only be achieved by scaling another font size
- shown in normal face}
-
-
- {$S AInit}
- PROCEDURE TDemoTextApplication.IDemoTextApplication;
- VAR err: OSErr;
- fntName: Str255;
- BEGIN
- (*InitPrinting;*)
-
- IApplication(kFileType);
-
- {Find out what applFont maps to, so that we set gFontNum to a real
- font number.}
- GetFontName(applFont, fntName);
- GetFNum(fntName, gFontNum);
- gFontSize := 10;
-
- IF NOT gFinderPrinting THEN
- BEGIN
- AddResMenu(GetMHandle(mFont), 'FONT');
-
- OutlineFontSizes(gFontNum);
- gStaggerCount := 0;
-
- {Set style-items to corresponding typeface}
- SetStyle(cBold, [bold]);
- SetStyle(cUnderline, [underline]);
- SetStyle(cItalic, [italic]);
- SetStyle(cOutline, [outline]);
- SetStyle(cShadow, [shadow]);
- SetStyle(cCondense, [condense]);
- SetStyle(cExtend, [extend]);
- END;
- END;
-
-
- {$S AOpen}
- FUNCTION TDemoTextApplication.DoMakeDocument(itsCmdNumber: CmdNumber):
- TDocument; OVERRIDE;
- VAR aTextDocument: TTextDocument;
-
- BEGIN
- New(aTextDocument);
- FailNIL(aTextDocument);
- aTextDocument.ITextDocument;
- DoMakeDocument := aTextDocument;
- END;
-
-
- {$IFC qDebug}
- {$S ADebug}
- PROCEDURE TDemoTextApplication.IdentifySoftware;
- BEGIN
- WriteLn('DemoText Source date: 23 April 86; Compiled: ',
- COMPDATE, ' @ ', COMPTIME);
- INHERITED IdentifySoftware;
- END;
- {$ENDC}
-
-
- {$S AOpen}
- PROCEDURE TTextDocument.ITextDocument;
- BEGIN
- fText := NIL;
- IDocument(kFileType, kSignature, kUsesDataFork, kUsesRsrcFork,
- NOT kDataOpen, NOT kRsrcOpen);
-
- fTEView := NIL;
- fText := NewPermHandle(0);
- FailNIL(fText);
- END;
-
-
- {$S AClose}
- PROCEDURE TTextDocument.Free; OVERRIDE;
- BEGIN
- IF fText <> NIL THEN
- DisposHandle(fText);
- INHERITED Free;
- END;
-
-
- {$S AOpen}
- PROCEDURE TTextDocument.DoInitialState; OVERRIDE;
-
- BEGIN
- WITH fTextSpecs DO
- BEGIN
- theFontNumber := gFontNum;
- theFontSize := gFontSize;
- theStyle := [];
- theJustification := teJustLeft;
- END;
- fSpecsChanged := TRUE;
- END;
-
-
- {$S AOpen}
- PROCEDURE TTextDocument.DoMakeViews(forPrinting: BOOLEAN); OVERRIDE;
- VAR aTEView: TTEView;
- itsExtent: Rect;
- aHandler: TStdPrintHandler;
-
- BEGIN
- New(aTEView);
- FailNIL(aTEView);
- aTEView.ITEView(
- NIL, {parent}
- SELF, {its document}
- fText, {its Text}
- Point(0), {location of its top-left-most point}
- cTyping, {its key-command number}
- 10, {10 pixels of margin on each side}
- 8, {8 pixels margin at top}
- 1000, {1000 pixels initial view width -- will be
- adjusted to be width of page later}
- 100, {100 pixels initial height -- will be
- adjusted to height-of-content later}
- fTextSpecs.theFontNumber,{font family to use}
- fTextSpecs.theFontSize, {font size to use}
- fTextSpecs.theStyle, {style to use}
- sizePage, {view width determined (initially) by page-
- width less desired margins}
- sizeVariable, {height determined (initially) by amount of
- text}
- kUnlimited {no limit to number of characters accepted}
- );
-
- aTEView.fHTE^^.just := fTextSpecs.theJustification;
-
- New(aHandler);
- FailNIL(aHandler);
- aHandler.IStdPrintHandler(aTEView, FALSE);
- aHandler.fMinimalMargins := FALSE;
-
- {$IFC qDebug}
- aTEView.fShowBorders := TRUE;
- aTEView.fShowExtraFeedback := TRUE;
- {$ENDC}
- fTEView := aTEView;
- END;
-
-
- {$S AOpen}
- PROCEDURE TTextDocument.DoMakeWindows; OVERRIDE;
- VAR
- aWindow: TWindow;
- BEGIN
- aWindow := NewSimpleWindow(kWindowRsrcID, NOT kDialogWindow,
- kWantHScrollBar, kWantVScrollBar, fTEView);
- AdaptToScreen(aWindow);
- SimpleStagger(aWindow, kStaggerAmount, kStaggerAmount, gStaggerCount);
- END;
-
-
- {$S ASelCommand}
- FUNCTION TTextDocument.DoMenuCommand(aCmdNumber: CmdNumber): TCommand;
- VAR sd: SizeDeterminer;
- aName: Str255;
- menu: INTEGER;
- item: INTEGER;
- newSpecs: TextSpecs;
- aStyleItem: StyleItem;
- PROCEDURE InstallChangedDeterminer(vhs: VHSelect);
- BEGIN
- IF sd <> fTEView.fSizeDeterminer[vhs] THEN
- BEGIN
- fTEView.fSizeDeterminer[vhs] := sd;
- {If we changed the horizontal size determiner, we
- must ask the TTEView to recompute the TE
- rectangles.}
- IF vhs = h THEN
- BEGIN
- IF sd = sizeFrame THEN
- fTEView.FrameChangedSize
- ELSE IF sd = sizePage THEN
- fTEView.DoPagination;
- END;
- fTEView.AdjustExtent;
- fTEView.fFrame.ForceRedraw;
- END;
- END;
-
- PROCEDURE LaunchTextCommand(aCmdNumber: CmdNumber);
- VAR aTextCommand: TTextCommand;
-
- BEGIN
- New(aTextCommand);
- FailNIL(aTextCommand);
- WITH newSpecs DO
- aTextCommand.ITextCommand(aCmdNumber, SELF, fTEView,
- theFontNumber, theFontSize,
- theStyle, theJustification);
- DoMenuCommand := aTextCommand;
- END;
- BEGIN
- DoMenuCommand := gNoChanges;
-
- newSpecs := fTextSpecs;
-
- CmdToMenuItem(aCmdNumber, menu, item);
- IF (cSizeMin <= aCmdNumber) AND (aCmdNumber <= cSizeMax) THEN
- BEGIN
- newSpecs.theFontSize := aCmdNumber - cSizeBase;
- LaunchTextCommand(cSizeChange);
- END
-
- ELSE
- IF menu = mFont THEN
- BEGIN
- GetItem(GetMHandle(menu), item, aName);
- GetFNum(aName, newSpecs.theFontNumber);
- LaunchTextCommand(cFontChange);
- END
-
- ELSE
- IF (aCmdNumber >= cJustLeft) AND (aCmdNumber <= cJustRight) THEN
- BEGIN
- WITH newSpecs DO
- IF aCmdNumber = cJustLeft THEN
- theJustification := teJustLeft
- ELSE
- IF aCmdNumber = cJustCenter THEN
- theJustification := teJustCenter
- ELSE
- theJustification := teJustRight;
-
- LaunchTextCommand(cJustChange);
- END
-
- ELSE
- IF (aCmdNumber = cPlainText) THEN
- BEGIN
- newSpecs.theStyle := [];
- LaunchTextCommand(cStyleChange);
- END
-
- ELSE
- IF (aCmdNumber > cPlainText) AND (aCmdNumber <= cExtend) THEN
- BEGIN
- CASE aCmdNumber OF
- cBold: aStyleItem := bold;
- cItalic: aStyleItem := italic;
- cUnderline: aStyleItem := underline;
- cOutline: aStyleItem := outline;
- cShadow: aStyleItem := shadow;
- cCondense: aStyleItem := condense;
- cExtend: aStyleItem := extend;
- END; {case}
- WITH newSpecs DO
- IF aStyleItem IN theStyle THEN
- theStyle := theStyle - [aStyleItem]
- ELSE
- theStyle := theStyle + [aStyleItem];
- LaunchTextCommand(cStyleChange);
- END
-
- ELSE
- IF (aCmdNumber <= cWidthOnePage) AND (aCmdNumber >= cWidthFrame) THEN
- BEGIN
- IF aCmdNumber = cWidthFrame THEN
- sd := sizeFrame
- ELSE
- IF aCmdNumber = cWidthOnePage THEN
- sd := sizePage
- ELSE
- sd := sizeFixed;
-
- {NB: The following is not undoable in the current version}
- InstallChangedDeterminer(h);
- END
-
- ELSE
- IF (aCmdNumber >= cHeightFrame) AND (aCmdNumber <= cHeightConst) THEN
- BEGIN
- IF aCmdNumber = cHeightFrame THEN
- sd := sizeFrame
- ELSE
- IF aCmdNumber = cHeightPages THEN
- sd := sizeFillPages
- ELSE
- IF aCmdNumber = cHeightText THEN
- sd := sizeVariable
- ELSE
- IF aCmdNumber = cHeightConst THEN
- sd := sizeFixed;
-
- {NB: The following is not undoable in the current version}
- InstallChangedDeterminer(v);
- END
-
- ELSE
- DoMenuCommand := INHERITED DoMenuCommand(aCmdNumber);
- END;
-
-
-
- {$S AWriteFile}
- PROCEDURE TTextDocument.DoNeedDiskSpace(VAR dataForkBytes,
- rsrcForkBytes: LONGINT);
- BEGIN
- dataForkBytes := dataForkBytes + GetHandleSize(fText);
-
- rsrcForkBytes := rsrcForkBytes +
- SIZEOF(TextSpecs) + kRsrcTypeOverhead + kRsrcOverhead +
- kPrintInfoSize + kRsrcTypeOverhead + kRsrcOverhead;
-
- INHERITED DoNeedDiskSpace(dataForkBytes, rsrcForkBytes);
- END;
-
-
- {$S AReadFile}
- PROCEDURE TTextDocument.DoRead(aRefNum: INTEGER;
- rsrcExists, forPrinting: BOOLEAN);
- VAR numChars: LONGINT;
- hTextSpecs: TextSpecsHdl;
- hPrintInfo: Handle;
-
- BEGIN
- {Read the text}
- FailOSErr(GetEOF(aRefNum, numChars));
-
- {The file may have been created by someone else--make sure we don't
- read more than we can handle}
- IF numChars > kUnlimited THEN
- BEGIN
- gApplication.ShowError(0, msgAlert + kFileTooBig);
- numChars := kUnlimited;
- END;
-
- SetHandleSize(fText, numChars);
- FailMemError;
- FailOSErr(FSRead(aRefNum, numChars, fText^));
-
- {Read the text specs resource}
- hTextSpecs := TextSpecsHdl(GetResource(kTextSpecsRsrcType,
- kTextSpecsRsrcID));
- IF hTextSpecs <> NIL THEN
- fTextSpecs := hTextSpecs^^
- ELSE
- WITH fTextSpecs DO
- BEGIN
- theFontNumber := gFontNum;
- theFontSize := gFontSize;
- theStyle := [];
- theJustification := teJustLeft;
- END;
- fSpecsChanged := TRUE;
-
- {Read the print info resource}
- hPrintInfo := GetResource(kPrintInfoRsrcType, kPrintInfoRsrcID);
- IF hPrintInfo <> NIL THEN {no print info resources was saved}
- BEGIN
- IF fPrintInfo = NIL THEN
- BEGIN
- fPrintInfo := NewPermHandle(kPrintInfoSize);
- FailNIL(fPrintInfo);
- END;
- BlockMove(hPrintInfo^, fPrintInfo^, kPrintInfoSize);
- END;
- END;
-
-
- {$S ARes}
- PROCEDURE TTextDocument.DoSetupMenus;
- VAR sd: SizeDeterminer;
- just: INTEGER;
- item: INTEGER;
- fnt: INTEGER;
- c: INTEGER;
- aName: Str255;
- aMenuHandle: MenuHandle;
- aStyle: Style;
- BEGIN
- INHERITED DoSetupMenus;
-
- aMenuHandle := GetMHandle(mFont);
- FOR item := 1 TO CountMItems(aMenuHandle) DO
- BEGIN
- {There can be more than 31 menu entries with scrolling menus,
- but trying to enable an item with number > 31 is bad news.
- If the menu itself is enabled (which it will be in MacApp
- if any of the first 31 items is enabled), then the extras
- will always be enabled.}
- IF item <= 31 THEN
- EnableItem(aMenuHandle, item);
- IF fSpecsChanged THEN
- BEGIN
- GetItem(aMenuHandle, item, aName);
- GetFNum(aName, fnt);
- IF fnt = fTextSpecs.theFontNumber THEN
- fCurrFontMenu := item;
- END;
- END;
- CheckItem(aMenuHandle, fCurrFontMenu, TRUE);
-
- FOR c := cSizeMin TO cSizeMax DO
- EnableCheck(c, TRUE, (c - cSizeBase) = fTextSpecs.theFontSize);
-
- aStyle := fTextSpecs.theStyle;
- EnableCheck(cPlainText, TRUE, aStyle = []);
- EnableCheck(cBold, TRUE, bold IN aStyle);
- EnableCheck(cItalic, TRUE, italic IN aStyle);
- EnableCheck(cUnderline, TRUE, underline IN aStyle);
- EnableCheck(cOutline, TRUE, outline IN aStyle);
- EnableCheck(cExtend, TRUE, extend IN aStyle);
- EnableCheck(cCondense, TRUE, condense IN aStyle);
- EnableCheck(cShadow, TRUE, shadow IN aStyle);
-
- sd := fTEView.fSizeDeterminer[h];
- EnableCheck(cWidthFrame, TRUE, (sd = sizeFrame));
- EnableCheck(cWidthOnePage, TRUE, (sd = sizePage));
- EnableCheck(cWidthView, TRUE, (sd = sizeFixed));
-
- sd := fTEView.fSizeDeterminer[v];
- EnableCheck(cHeightFrame, TRUE, (sd = sizeFrame));
- EnableCheck(cHeightPages, TRUE, (sd = sizeFillPages));
- EnableCheck(cHeightText, TRUE, (sd = sizeVariable));
- EnableCheck(cHeightConst, TRUE, (sd = sizeFixed));
-
- just := fTextSpecs.theJustification;
- EnableCheck(cJustLeft, TRUE, (just = teJustLeft));
- EnableCheck(cJustCenter, TRUE, (just = teJustCenter));
- EnableCheck(cJustRight, TRUE, (just = teJustRight));
-
- IF fSpecsChanged THEN
- OutlineFontSizes(fTextSpecs.theFontNumber);
-
- fSpecsChanged := FALSE;
- END;
-
-
- {$S AWriteFile}
- PROCEDURE TTextDocument.DoWrite(aRefNum: INTEGER; makingCopy: BOOLEAN);
- VAR numChars: LONGINT;
- hTextSpecs: TextSpecsHdl;
- tempHandle: Handle;
- BEGIN
- {Write out the text}
- numChars := GetHandleSize(fText);
- FailOSErr(FSWrite(aRefNum, numChars, fText^));
-
- {Write the text specification resource, after converting it to a handle}
- hTextSpecs := TextSpecsHdl(NewHandle(SIZEOF(TextSpecs)));
- FailNIL(hTextSpecs);
- hTextSpecs^^ := fTextSpecs;
- AddResource(Handle(hTextSpecs), kTextSpecsRsrcType,
- kTextSpecsRsrcID, '');
- FailResError;
-
- {Write the print info resource. Note we can't use MacApp for this
- because MacApp will write the print info into the data fork.
- Note also--we must copy the print info resource to another handle
- because the Resource Manager will dispose of the resource handles
- when a resource fork is closed.}
- tempHandle := fPrintInfo;
- FailOSErr(HandTohand(tempHandle));
- AddResource(tempHandle, kPrintInfoRsrcType, kPrintInfoRsrcID, '');
- FailResError;
- END;
-
-
- {$S AClose}
- PROCEDURE TTextDocument.FreeData; OVERRIDE;
- BEGIN
- SetHandleSize(fText, 0);
- END;
-
-
- {$S ARes}
- PROCEDURE TTextDocument.InstallTextSpecs(specs: TextSpecs);
-
- VAR
- savedPort: GrafPtr;
- tempPort: GrafPort;
- newInfo: FontInfo;
-
- BEGIN
- {Make sure the font info record is correct. Note that we may have
- to do this when the window isn't around, so roll our own
- GrafPort.}
- GetPort(savedPort);
- OpenPort(@tempPort);
- WITH specs DO
- BEGIN
- TextFont(theFontNumber);
- TextSize(theFontSize);
- TextFace(theStyle);
- END;
- GetFontInfo(newInfo);
- ClosePort(@tempPort);
- SetPort(savedPort);
-
- fTextSpecs := specs;
- fSpecsChanged := TRUE;
-
- WITH fTEView, specs DO
- BEGIN
- fFont := theFontNumber;
- fSize := theFontSize;
- fFontInfo := newInfo;
- WITH fHTE^^ DO
- BEGIN
- WITH newInfo DO
- BEGIN
- fontAscent := ascent;
- lineHeight := ascent+descent+leading;
- END;
- txSize := theFontSize;
- txFont := theFontNumber;
- txFace := theStyle;
- just := theJustification;
- fFrame.fScrollUnit.v := lineHeight;
- END;
- END;
- fTEView.RecalcText;
- fTEView.DoPagination;
- END;
-
-
- {$S AReadFile}
- PROCEDURE TTextDocument.ShowReverted;
-
- BEGIN
- InstallTextSpecs(fTextSpecs);
- TESetSelect(0, 0, fTEView.fHTE);
- INHERITED ShowReverted;
- END;
-
-
-
- {$S ARes}
- PROCEDURE OutlineFontSizes(fontNum: INTEGER);
- VAR c: CmdNumber;
- BEGIN
- FOR c := cSizeMin TO cSizeMax DO
- BEGIN
- IF RealFont(fontNum, c - cSizeBase) THEN
- SetStyle(c, [outline])
- ELSE
- SetStyle(c, []);
- END;
- END;
-
-
- {$S ASelCommand}
- PROCEDURE TTextCommand.ITextCommand(itsCmdNumber: CmdNumber;
- itsTextDocument: TTextDocument;
- itsTEView: TTEView;
- itsFontNumber: INTEGER;
- itsFontSize: INTEGER;
- itsStyle: Style;
- itsJustification: INTEGER);
- VAR info: FontInfo;
- BEGIN
- ICommand(itsCmdNumber);
- fTEView := itsTEView;
- fTextDocument := itsTextDocument;
- WITH fOldTextSpecs, itsTEView.fHTE^^ DO
- BEGIN
- theFontNumber := txFont;
- theFontSize := txSize;
- theStyle := txFace;
- theJustification := just;
- END;
-
- WITH fNewTextSpecs DO
- BEGIN
- theFontNumber := itsFontNumber;
- theFontSize := itsFontSize;
- theStyle := itsStyle;
- theJustification := itsJustification;
- END;
- END;
-
-
- {$S ADoCommand}
- PROCEDURE TTextCommand.DoIt; OVERRIDE;
- BEGIN
- fTextDocument.InstallTextSpecs(fNewTextSpecs);
- END;
-
-
- {$S ADoCommand}
- PROCEDURE TTextCommand.UndoIt; OVERRIDE;
- BEGIN
- fTextDocument.InstallTextSpecs(fOldTextSpecs);
- END;
-
-
- {$S ADoCommand}
- PROCEDURE TTextCommand.RedoIt; OVERRIDE;
- BEGIN
- fTextDocument.InstallTextSpecs(fNewTextSpecs);
- END;
-
-
-
-